import { Component, forwardRef, Inject, Injector, OnInit } from '@angular/core';
import { Host } from '../../../service/config.service';
import { MyTabPanel } from '../../public/tabview/tabview.component';
import { BaseTermTabComponent } from '../base-term-tab/base-term-tab.component';

@Component({
  selector: 'app-forward',
  templateUrl: './forward.component.html',
  styleUrls: ['./forward.component.css']
})
export class ForwardComponent extends BaseTermTabComponent implements OnInit {

  constructor(@Inject(forwardRef(() => MyTabPanel)) tabPanel: MyTabPanel, protected injector: Injector) {
    super(tabPanel, injector);
  }

  newForwardSession(ip) {
    this.newSession(
      (host) => {
        let lastHost: Host = this.host;
        while (lastHost.child) lastHost = lastHost.child;
        this.xterm.writeln(`Forwording is working.`);
        this.xterm.writeln(`IP:${lastHost.ip}, Port:${lastHost.port}, LocalPort: ${host.localPort}\r\n`);
        if (lastHost.url) {
          let url = lastHost.url.replace('$IP', ip + ':' + host.localPort);
          this.xterm.writeln(url);
        }
      },
      (data) => {
        //this.recvNewData = true;
        //this.xterm.writeln(`new connection,  IP:${data.ip}, Port:${data.port}`);
      },
      (error) => {
        this.xterm.write("\r\n\r\n");
        this.xterm.writeln("\x1b[1;1;31mError:" + error + "\x1b[0m");
      }
    );
  }
  ngOnInit(): void {
    this.sessionService.localSession.getSysParam().then((param: any) => {
      this.newForwardSession(param.ip);
    })
  }

}
